class snake{
public:
    snake()
	{
		x[0]=10;
		x[1]=11;
		y[0]=10;
		y[1]=10;
		lenth=2;
		direction=1;
		hd=0;
		dx=0;
		dy=0;
		health=0;
		m=0;
	}//默认构造函数
	void gotoxy(int x,int y)  
	{
    CONSOLE_SCREEN_BUFFER_INFO    csbiInfo;                            
    HANDLE    hConsoleOut;
    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
    csbiInfo.dwCursorPosition.X = x;                                    
    csbiInfo.dwCursorPosition.Y = y;                                    
    SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);   
	}//goto函数定义，（0,0）为左上角
	bool move()
	{
		if (hd==0)
		{
			while (dx=rand()%(N1-1),dx==0 || dx==N1-1 ||dx==x[0]){};
			while (dy=rand()%(N2-1),dy==0 || dy==N2-1 ||dy==x[0]){};
			hd=1;
		}
		x[lenth]=x[lenth-1];
		y[lenth]=y[lenth-1];
		for(int i=lenth-1;i!=0;i--)
		{
		x[i]=x[i-1];
		y[i]=y[i-1];
		}
		switch (direction)
		{
		case 1:y[0]--;break;
		case 2:x[0]++;break;
		case 3:y[0]++;break;
		case 4:x[0]--;break;
		default:break;
		}
		if (x[0]==dx &&y[0]==dy)
		{
		lenth++;
		hd=0;
		gotoxy(dx,dy);
		std::cout<<" ";
		}
		memset(Map,0,sizeof(Map));
		for (int i=0;i!=lenth;i++)
		{
		Map[x[i]][y[i]]=1;
		}
		for (int i=0;i!=N1;i++){
		Map[i][0]=2;
		Map[i][N2-1]=2;
		}
		for (int i=0;i!=N2;i++){
		Map[0][i]=2;
		Map[N1-1][i]=2;
		}
		Map[dx][dy]=3;
		return condition();
	}
	bool condition(){
		for (int i=1;i!=lenth;i++){
			if (x[0]==x[i] && y[0]==y[i]){
			return 1;
			}
		}
		if (x[0]==0 || x[0]==N1-1 ||y[0]==0 || y[0]==N2-1){
		   return 1;
		}
	    return 0;
	}
	void change(){
	if (GetAsyncKeyState(VK_UP)<0) direction=1;
	if (GetAsyncKeyState(VK_DOWN)<0) direction=3;
	if (GetAsyncKeyState(VK_LEFT)<0) direction=4;
	if (GetAsyncKeyState(VK_RIGHT)<0) direction=2;
	}
	void ot1()
	{
     if (m==0)
	 {
		for (int j=0;j!=N2;j++)
		{
			if (m!=0) std::cout<<std::endl; 
			m++;
			for (int i=0;i!=N1;i++)
			{
		if (Map[i][j]==1) std::cout<<"@";
		else if (Map[i][j]==2) std::cout<<"*";
		else if (Map[i][j]==3) std::cout<<"0";
		else std::cout<<" ";
			}
		}
	m++;
	 }
	 else {
		gotoxy(x[lenth],y[lenth]);
		std::cout<<" ";
		gotoxy(x[0],y[0]);
		std::cout<<"@";
		gotoxy(dx,dy);
		std::cout<<"0";
		gotoxy(N1,N2);}
	}
private:
	int x[100];
	int y[100];
	int lenth;
	int direction;
	int health;
	static const int N1=75;
	static const int N2=20;
	int Map[N1][N2];//地图坐标，0为空，1为蛇，2为墙，3为豆子
	int dx,dy;//豆子坐标
	int hd;//豆子是否存在
	int m;
};